package sanitycheck;

import java.io.File;
import java.util.Hashtable;
import java.util.Map.Entry;

import setting.BioNLPSettings;
import def.Argument;
import def.BioNLPDocument;
import def.Event;
import exp.Count;

public class RequireCoref {
	public static void main(String[] args){
		BioNLPSettings.init(args[0]);
		BioNLPSettings.print();
		Count sum = new Count();
		String path = BioNLPSettings.getValue("dataPath");
		for (String filename :  new File(path).list()){
			//		String filename = "9794375.txt";
			if (filename.endsWith(".txt"))
				sum.add(processDocument(path, filename.replace(".txt", "")));
		}
		System.out.println(sum);
	}

	public static Count processDocument(String path, String filePrefix){
		Count c = new Count();
		Argument.clear();

		BioNLPDocument doc = new BioNLPDocument(path,filePrefix);

		if (!doc.valid)
			return c;

		// check if needs coref
		Hashtable<Event, Integer> freq = new Hashtable<Event, Integer>();
		if (doc.events == null){
			return c;
		}
		// first pass
		System.err.println("[1st]"+freq);
		for (Event event: doc.events.values()){
			c.all++;
			freq.put(event, 0);

			int sentId = doc.getSentenceIdByPos(event.getTspan().startIdx);
			for (Argument arg:event.args){
				if (/*!(arg instanceof Event) && */(arg.getTspan().startIdx < doc.aText.sentStartPos[sentId] || 
						arg.getTspan().startIdx > doc.aText.sentEndPos[sentId])){
					c.num++;
					freq.put(event, 1);
					System.out.println("[FILE]"+filePrefix);
					System.out.println("EVENT: "+event);
					System.out.println("Sentence@"+sentId +" :"+ doc.aText.lines[sentId]);
					System.out.println(arg.getTspan().startIdx +", " + doc.aText.sentStartPos[sentId]+"; "
							+arg.getTspan().startIdx + ", "+doc.aText.sentEndPos[sentId]);
					System.out.println("============================");
					break;
				}
			}
		}
		// second pass
		System.err.println("[2nd]"+freq);
		for (Event event: doc.events.values()){

			if (freq.get(event)==1)
				continue;
			System.err.println("[2nd]"+ event );
			int sentId = doc.aText.getSentenceIdByPos(event.getTspan().startIdx);
			for (Argument arg:event.args){
				if (arg instanceof Event){
					System.err.println("[]"+ event + "   "+ arg);
					if (arg.getTspan().startIdx < doc.aText.sentStartPos[sentId] 
					                                                     || arg.getTspan().startIdx > doc.aText.sentEndPos[sentId]){
						freq.put(event, 1);
						c.num++;
						System.out.println("[FILE]"+filePrefix);
						System.out.println("EVENT: "+event);
						System.out.println("Sentence@"+sentId +" :"+ doc.aText.lines[sentId]);
						System.out.println("Argument : "+arg);
						System.out.println(arg.getTspan().startIdx +", "+doc.aText.sentStartPos[sentId]+"; "
								+arg.getTspan().startIdx + ", "+doc.aText.sentEndPos[sentId]);
						System.out.println("============================");
						break;
					}
					else{
						int s = 0;
						for (Entry<Event, Integer> entry: freq.entrySet()){
							if (entry.getKey() ==  arg){
								s = entry.getValue();
								System.err.println("[]"+ event + "   "+ arg+"  found inside args s="+s);
							}
						}
						if (s > 0){

							c.num+=s;
							freq.put(event, s);
							System.out.println("[FILE]"+filePrefix);
							System.out.println("EVENT: "+event);
							System.out.println("Sentence@"+sentId +" :"+ doc.aText.lines[sentId]);
							System.out.println("Argument : "+arg);
							System.out.println(" argument require coref itself");
							System.out.println("============================");
							break;
						}
					}

				}
			}
		}

		// third pass

		for (Event event: doc.events.values()){
			if (freq.get(event)==1)
				continue;
			int sentId = doc.getSentenceIdByPos(event.getTspan().startIdx);
			for (Argument arg:event.args){
				if (arg instanceof Event){
					if (arg.getTspan().startIdx < doc.aText.sentStartPos[sentId] || arg.getTspan().startIdx > doc.aText.sentEndPos[sentId]){
						freq.put(event, 1);
						c.num++;
						System.out.println("[FILE]"+filePrefix);
						System.out.println("EVENT: "+event);
						System.out.println("Sentence@"+sentId +" :"+ doc.aText.lines[sentId]);
						System.out.println("Argument : "+arg);
						System.out.println(arg.getTspan().startIdx +", "+doc.aText.sentStartPos[sentId]+"; "+arg.getTspan().startIdx + ", "+doc.aText.sentEndPos[sentId]);
						System.out.println("============================");
						break;
					}
					else{
						int s = 0;
						for (Entry<Event, Integer> entry: freq.entrySet()){
							if (entry.getKey() ==  arg){
								s = entry.getValue();
							}
						}
						if (s > 0){
							c.num+=s;
							freq.put(event, s);
							System.out.println("[FILE]"+filePrefix);
							System.out.println("EVENT: "+event);
							System.out.println("Sentence@"+sentId +" :"+ doc.aText.lines[sentId]);
							System.out.println("Argument : "+arg);
							System.out.println(" argument require coref itself");
							System.out.println("============================");
							break;
						}
					}

				}
			}
		}

		// 4th 
		for (Event event: doc.events.values()){
			if (freq.get(event)==1)
				continue;
			int sentId = doc.getSentenceIdByPos(event.getTspan().startIdx);
			for (Argument arg:event.args){
				if (arg instanceof Event){
					if (arg.getTspan().startIdx < doc.aText.sentStartPos[sentId] || arg.getTspan().startIdx > doc.aText.sentEndPos[sentId]){
						freq.put(event, 1);
						c.num++;
						System.out.println("[FILE]"+filePrefix);
						System.out.println("EVENT: "+event);
						System.out.println("Sentence@"+sentId +" :"+ doc.aText.lines[sentId]);
						System.out.println("Argument : "+arg);
						System.out.println(arg.getTspan().startIdx +", "+doc.aText.sentStartPos[sentId]+"; "+arg.getTspan().startIdx + ", "+doc.aText.sentEndPos[sentId]);
						System.out.println("============================");
						break;
					}
					else{
						int s = 0;
						for (Entry<Event, Integer> entry: freq.entrySet()){
							if (entry.getKey() ==  arg){
								s = entry.getValue();
							}
						}
						if (s > 0){
							c.num+=s;
							freq.put(event, s);
							System.out.println("[FILE]"+filePrefix);
							System.out.println("EVENT: "+event);
							System.out.println("Sentence@"+sentId +" :"+ doc.aText.lines[sentId]);
							System.out.println("Argument : "+arg);
							System.out.println(" argument require coref itself");
							System.out.println("============================");
							break;
						}
					}

				}
			}
		}
		//		
		//		for (Event e: freq.keySet()){
		//			c.all++;
		//			c.num += freq.get(e);
		//		}
		return c;
	}


}


